package com.javaclimb.filmbuy.controller;

import com.github.pagehelper.PageInfo;
import com.javaclimb.filmbuy.common.Consts;
import com.javaclimb.filmbuy.common.Result;
import com.javaclimb.filmbuy.common.ResultCode;
import com.javaclimb.filmbuy.entity.Account;
import com.javaclimb.filmbuy.entity.GoodsInfo;
import com.javaclimb.filmbuy.entity.CommentInfo;
import com.javaclimb.filmbuy.exception.CustomException;
import com.javaclimb.filmbuy.service.GoodsInfoService;
import com.javaclimb.filmbuy.service.CommentInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/*
* 评价的controller层
* */
@RestController
@RequestMapping(value = "/CommentInfo")
public class CommentInfoController {
    @Resource
    private CommentInfoService commentInfoService;


    /*
    * 增加一个评价
    * */
    @PostMapping
    public Result<CommentInfo> add(@RequestBody CommentInfo commentInfo,HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute(Consts.USER);
        if(user==null){
            throw new CustomException("1001","session已失效，请重新登录");
        }
        commentInfo.setUserId(user.getId());
        commentInfoService.add(commentInfo);
        return Result.success(commentInfo);
    }

    /*
    * 根据主键id删除一个评价
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        commentInfoService.delete(id);
        return Result.success();
    }

    /*
    * 更新一个评价
    * */
    @PutMapping
    public Result<CommentInfo> update(@RequestBody CommentInfo commentInfo) {
        commentInfoService.update(commentInfo);
        return Result.success(commentInfo);
    }

    /*
     * 根据主键id查询一个评价
     * */
    @GetMapping("/{id}")
    public Result<CommentInfo> findById(@PathVariable Long id){
        CommentInfo commentInfo = commentInfoService.findById(id);
        return Result.success(commentInfo);
    }

    /**
     * 查询所有的评价
     */
    @GetMapping
    public Result<List<CommentInfo>> findAll(){
        commentInfoService.findAll();
        return Result.success(commentInfoService.findAll());
    }

    /**
     * 查询某个电影的所有评价
     */
    @GetMapping("/all/{goodsId}")
    public Result<List<CommentInfo>> findAllByGoodsId(@PathVariable Long goodsId){
        commentInfoService.findAll();
        return Result.success(commentInfoService.findAll(goodsId));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page/{content}")
    public Result<PageInfo<CommentInfo>> page(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "5") Integer pageSize,
                                              @PathVariable String content,
                                              HttpServletRequest request){
        return Result.success(commentInfoService.findPage(pageNum,pageSize,content,request));
    }



}
